package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.UserLogin;
import cn.wolfcode.shop.domian.OrderInfo;
import cn.wolfcode.shop.exception.UserException;
import cn.wolfcode.shop.producer.CreateOrderInfoProducer;
import cn.wolfcode.shop.service.IOrderInfoService;
import cn.wolfcode.shop.util.RedisConstants;
import cn.wolfcode.shop.vo.CreateOrderVo;
import cn.wolfcode.shop.vo.JSONResultVo;
import cn.wolfcode.shop.vo.OrderStatusChangeVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jms.JmsException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.jms.JMSException;
import java.text.MessageFormat;

@RestController
@RequestMapping("/api/orders")
public class OrderController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private CreateOrderInfoProducer orderInfoProducer;
    @Reference
    private IOrderInfoService orderInfoService;
    @PostMapping
    public JSONResultVo generateOrder(@RequestHeader("user-token") String token , @RequestBody CreateOrderVo createOrderVo){
        JSONResultVo vo = new JSONResultVo();
        try {
            //通过token获取当前登录用户
            String userKey = MessageFormat.format(RedisConstants.USER, token);
            UserLogin userLogin = (UserLogin) redisTemplate.opsForValue().get(userKey);
            if (userLogin == null) {
                vo.setErrorMsg("用户信息已过期,请重新登录");
                return vo;
            }
//            OrderInfo orderInfo = orderInfoService.generateOrder(token, createOrderVo);
            orderInfoProducer.sendMessage(userLogin.getId(), createOrderVo);
            vo.setResult("订单已下达");
        } catch (UserException e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        } catch (JmsException je){
            je.printStackTrace();
            vo.setErrorMsg("系统异常,请联系管理员");
        }
        return vo;
    }

    @PostMapping("changeStauts")
    public JSONResultVo changeStatus(@RequestHeader("user-token") String token , @RequestBody OrderStatusChangeVo orderStatusChangeVo){
        JSONResultVo vo = new JSONResultVo();
        try {
            //通过token获取当前登录用户
            String userKey = MessageFormat.format(RedisConstants.USER, token);
            UserLogin userLogin = (UserLogin) redisTemplate.opsForValue().get(userKey);
            if (userLogin == null) {
                vo.setErrorMsg("用户信息已过期,请重新登录");
                return vo;
            }
            orderStatusChangeVo.setUserName(userLogin.getUserName());
            OrderInfo orderInfo = orderInfoService.changeStatus(orderStatusChangeVo);
            vo.setResult(orderInfo);
        } catch (UserException e){
            e.printStackTrace();
            vo.setErrorMsg(e.getMessage());
        } catch (JmsException je){
            je.printStackTrace();
            vo.setErrorMsg("系统异常,请联系管理员");
        }
        return vo;
    }

}
